﻿#region

using System.IO;

#endregion

namespace CDNSync.Providers.Model
{
    public class FileUploadRequest
    {
        private bool compress;
        private string fileNamePrefix;
        private string fileNameSuffix;
        private string parentKey;
        private string sourceFilePath;
        private bool useFolderNameInKey;

        public FileUploadRequest(string sourceFilePath,
                                 bool useFolderNameInKey,
                                 string parentKey,
                                 string fileNameSuffix,
                                 string fileNamePrefix,
                                 bool compress)
        {
            this.sourceFilePath = sourceFilePath;
            this.useFolderNameInKey = useFolderNameInKey;
            this.parentKey = parentKey;
            this.fileNameSuffix = fileNameSuffix;
            this.fileNamePrefix = fileNamePrefix;
            this.compress = compress;

            var fullPath = Path.GetFullPath(sourceFilePath);

            if (!File.Exists(fullPath))
            {
                throw new FileNotFoundException();
            }

            var fileInfo = new FileInfo(fullPath);


            var fileName = fileInfo.Name;

            if (!string.IsNullOrEmpty(fileNamePrefix))
            {
                fileName = fileNamePrefix + fileName;
            }

            if (!string.IsNullOrEmpty(fileNameSuffix))
            {
                fileName = fileName.Replace(fileInfo.Extension, fileNameSuffix) + fileInfo.Extension;
            }

            var directoryName = parentKey != null ? Path.Combine(parentKey, fileInfo.Directory.Name) : fileInfo.Directory.Name;

            if (string.IsNullOrEmpty(TargetKey))
            {
                TargetKey = !useFolderNameInKey ? fileName : Path.Combine(directoryName, fileName);
            }

            TargetKey = TargetKey.Replace("\\", "/").Replace(" ", string.Empty);

            SourceFilePath = fullPath;
        }


        public string SourceFilePath { get; set; }

        public string TargetKey { get; set; }

        public string ContentType { get; set; }

        public bool Compress
        {
            get { return compress; }
        }
    }
}